/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial');

GBIF.GeoSpatial.FeaturePanel = function(config){
	
	this.qurablelayertree = new GBIF.GeoSpatial.QueryableLayerTree({
			region : 'west'
		,	title:'Qurable Layers'	
		,	width: '250'
		,	minWidth:'225'
		,	split:true	
	});

	this.tabmanager = new GBIF.GeoSpatial.FeatureTabManager({
		region : 'center'
	});

	this.qurablelayertree.on('click',this.loadfeatureGrid ,this)
	
	GBIF.GeoSpatial.BUS.on('addFeatures',this.addfeaturesToNode, this);
	
	Ext.apply(this,config,{	
			border:false
		,	layout:'border'
		,	proxyUrl:'../resources/api/proxy.php?url='	
		, items:[
					this.qurablelayertree
				,	this.tabmanager
			]
	});

	GBIF.GeoSpatial.FeaturePanel.superclass.constructor.call(this, config)

}

Ext.extend(GBIF.GeoSpatial.FeaturePanel,Ext.Panel, {

		loadfeatureGrid: function(node, evt){
			Ext.each(this.tabmanager.items.items,function(item){
				item = (typeof item == 'undefined') ? false : item;
				if (item) {
					if (item.itemId == node.text) {
						this.tabmanager.layout.setActiveItem(item);
						if(!item.loaded){
							item.loaded=true;
							var url = 'http://ogc.gbif.org/wfs&request=describefeaturetype&version=' + node.attributes.layer.version + '&outputformat=xmlschema';
							item.store.proxy.url = this.proxyUrl + url;
							item.store.load();		
						}
					}
				}
			}, this);
		}
		
	,	addfeaturesToNode:function(){
			var node = this.qurablelayertree.getSelectionModel().getSelectedNode();
			var features = this.featuregrid.getSelectionModel().getSelections();
			Ext.each(this.featuredata, function(item){
				var data = item.data;
				if(node.text==data.title){
					item.data.mtype = 'wms'
					item.data.layer.features = features;
				}
			}, this);
		}

});